package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dongdongshop.data.Result;
import com.dongdongshop.data.ResultEnum;
import com.dongdongshop.pojo.seller.TbSpecification;
import com.dongdongshop.pojo.seller.TbSpecificationOption;
import com.dongdongshop.service.SpecificationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.xml.ws.Response;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("specification")
public class SpecificationController {

    @Reference
    private SpecificationService specificationService;

    private final Logger logger= LoggerFactory.getLogger(SpecificationController.class);
    @RequestMapping("toList")
    public String toList(){
        return "admin/specification";
    }

    @RequestMapping("getInfo")
    @ResponseBody
    public Result getInfo(String specName){
        try {
            List<TbSpecification> list=specificationService.getInfo(specName);
            return Result.response(ResultEnum.OK).setData(list);
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    //删除
    @RequestMapping("deleteByIds")
    @ResponseBody
    public Result deleteByIds(@RequestParam("ids[]")Integer[] ids){
        try {
            int i=specificationService.deleteByIds(ids);
            if (i>0){
                return Result.response(ResultEnum.OK);
            }else {
                return Result.response(ResultEnum.FAIL);
            }
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    //增加
    @RequestMapping("add")
    @ResponseBody
    public Result add(TbSpecification specification,String json){
        try {
            List<TbSpecificationOption> list=(List<TbSpecificationOption>) JSON.parse(json);
            int addCount=specificationService.add(specification,list);
            if (addCount > 0) {
                return Result.response(ResultEnum.OK);
            }else{
                return Result.response(ResultEnum.FAIL);
            }
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    //回显
    @RequestMapping("getDataById/{id}")
    @ResponseBody
    public Result getDataById(@PathVariable("id")Long id){
        try {
            TbSpecification tbSpecification=specificationService.getDataById(id);
            return Result.response(ResultEnum.OK).setData(tbSpecification);
        }catch (Exception e){
            logger.error("操作异常",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
    //修改
    @RequestMapping("updateSpecification")
    @ResponseBody
    public Result updateSpecification(TbSpecification tbSpecification,String json){
        try{
            List<TbSpecificationOption> list = (List<TbSpecificationOption>) JSON.parse(json);
            System.out.println("tb"+tbSpecification+"json"+json);
            int count = specificationService.updateSpecification(tbSpecification,list);
            if(count >= 1){
                return Result.response(ResultEnum.OK);
            }else{
                return Result.response(ResultEnum.FAIL);
            }
        }catch(Exception e){
            logger.error("操作异常", e);
            return Result.response(ResultEnum.FAIL);
        }
    }
}
